#pragma  once

#include <iostream>
using namespace std;

enum Color{
	RED,
	BLACK
};

template<class K,class V>
struct RBTreeNode
{
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	pair<K, V> _kv;
	Color _col;

	RBTreeNode(const pair<K,V>& kv)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _kv(kv)
		, _col(RED)
	{}
};
template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	RBTree()
		:_root(nullptr)
	{}

	void Destory(Node* root)
	{
		if (nullptr == root)
			return;
		Destory(root->_left);
		Destory(root->_right);
		delete root;
	}

	~RBTree()
	{
		Destory(_root);
		_root = nullptr;
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur){
			if (key < cur->_kv.first)
				cur = cur->_left;
			else if (key > cur->_kv.first)
				cur = cur->_right;
			else
				return cur;
		}
		return nullptr;
	}

	pair<Node*, bool> Insert(const pair<K, V>& kv)
	{
		//空树
		if (_root == nullptr){
			_root = new Node(kv);
			_root->_col = BLACK;
			return make_pair(_root, true);
		}

		//非空
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur){
			if (cur->_kv.first < kv.first){
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return make_pair(cur, false);
			}
		}

		Node* newnode = new Node(kv);
		newnode->_col = RED;//新插入节点定义为红色
		if (parent->_kv.first < kv.first){
			parent->_right = newnode;
			newnode->_parent = parent;//三叉连链接
		}
		else //parent->_kv.first > kv.first
		{
			parent->_left = newnode;
			newnode->_parent = parent;
		}
		cur = newnode;

		//处理过程
		//父节点存在 且 为红色
		while (parent && parent->_col == RED){
			Node* grandfather = parent->_parent;//定义祖父节点
			//父节点是祖父节点左孩子 关键需要看uncle节点
			if (parent == grandfather->_left){
				Node* uncle = grandfather->_right;
				//处理1：uncle存在且为红
				if (uncle && uncle->_col == RED){
					//1)变色
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					//2）递推
					cur = grandfather;
					parent = cur->_parent;
				}
				else //处理2&3：uncle不存在 或 uncle存在且为黑
				{
					//处理2：单旋
					if (cur == parent->_left){
						RotateR(grandfather);
						grandfather->_col = RED;
						parent->_col = BLACK;
					}
					else//处理3：双旋
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else //parent == grandfather->_right
			{
				Node* uncle = grandfather->_left;
				// 情况1：
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else // 情况2：+ 情况3：
				{
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else // cur == parent->_left
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					// 插入结束
					break;
				}
			}
		}
		_root->_col = BLACK;//红黑树根节点为黑色
		return make_pair(newnode, true);
	}

	//旋转
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
		{
			subRL->_parent = parent;
		}

		subR->_left = parent;
		Node* parentParent = parent->_parent;
		parent->_parent = subR;

		if (parent == _root)
		{
			_root = subR;
			_root->_parent = nullptr;
		}
		else
		{
			if (parentParent->_left == parent)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		subL->_right = parent;
		Node* parentParent = parent->_parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
			if (parentParent->_left == parent)
				parentParent->_left = subL;
			else
				parentParent->_right = subL;

			subL->_parent = parentParent;
		}
	}

	//红黑树平衡检测
	bool _CheckBlance(Node* root, int blackNum, int count)
	{
		//空树
		if (root == nullptr)
		{
			if (count != blackNum) //黑色节点计算值 不等于 标准值 不等，不是红黑树
			{
				cout << "黑色节点数不等于标准值" << endl;
				return false;
			}
			return true;
		}

		//存在连续红色节点 false
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "存在连续的红色节点" << endl;
			return false;
		}

		if (root->_col == BLACK) 		//统计黑色节点数
		{
			count++;
		}

		//前序遍历
		return _CheckBlance(root->_left, blackNum, count)
			&& _CheckBlance(root->_right, blackNum, count);
	}

	bool CheckBlance()
	{
		//空树，是平衡
		if (_root == nullptr)
		{
			return true;
		}

		//根结点是黑色
		if (_root->_col == RED)
		{
			cout << "root is Red" << endl;
			return false;
		}

		// 找最左路径做黑色节点数量参考值
		int blackNum = 0;//统计黑色节点数量
		Node* left = _root;
		while (left)
		{
			if (left->_col == BLACK)
			{
				blackNum++;//标准值
			}

			left = left->_left;
		}

		int count = 0;//计算的每条路径
		return _CheckBlance(_root, blackNum, count);
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

private:
	Node* _root;
};